Higher-Order TypeScript (HOTScript)
A library of composable functions for the type level!
Transform your TypeScript types in any way you want using functions you already know.
Features
Type-level higher-order functions (Tuples.Map
, Tuples.Filter
, Objects.MapValues
, etc). Type-level pattern matching with Match
. Performant math operations (Numbers.Add
, Numbers.Sub
, Numbers.Mul
, Numbers.Div
, etc). Custom "lambda" functions.
🚧 work in progress 🚧
Installation
You can find HotScript on npm:
npm install -D hotscript
HotScript is a work-in-progress library, so expect breaking changes in its API.
Examples
Transforming a list
Run this as a TypeScript Playground
import { Pipe , Tuples , Strings , Numbers } from "hotscript" ;
type res1 = Pipe <
[1 , 2 , 3 , 4 ],
[
Tuples .Map <Numbers .Add <3 >>,
Tuples .Join <"." >,
Strings .Split <"." >,
Tuples .Map <Strings .Prepend <"1" >>,
Tuples .Map <Strings .ToNumber >,
Tuples .Sum
]
>;
Defining a first-class function
Run this as a TypeScript Playground
import { Call , Fn , Tuples } from "hotscript" ;
interface Duplicate extends Fn {
return : [this ["arg0" ], this ["arg0" ]];
}
type result1 = Call <Tuples .Map <Duplicate >, [1 , 2 , 3 , 4 ]>;
type result2 = Call <Tuples .FlatMap <Duplicate >, [1 , 2 , 3 , 4 ]>;
Transforming an object type
Run this as a TypeScript Playground
import { Pipe , Objects , Booleans } from "hotscript" ;
type ToAPIPayload <T> = Pipe <
T,
[
Objects .OmitBy <Booleans .Equals <symbol >>,
Objects .Assign <{ metadata : { newUser : true } }>,
Objects .SnakeCaseDeep ,
Objects .Assign <{ id : string }>
]
>;
type T = ToAPIPayload <{
id : symbol ;
firstName : string ;
lastName : string ;
}>;
type T = {
id : string ;
metadata : { new_user : true };
first_name : string ;
last_name : string ;
};
Parsing a route path
Run this as a TypeScript Playground
https://user-images.githubusercontent.com/2315749/222081717-96217cd2-ac89-4e06-a942-17fbda717cd2.mp4
import { Pipe , Objects , Strings , ComposeLeft , Tuples , Match } from "hotscript" ;
type res5 = Pipe <
"/users/<id:string>/posts/<index:number>" ,
[
Strings .Split <"/" >,
Tuples .Filter <Strings .StartsWith <"<" >>,
Tuples .Map <ComposeLeft <[Strings .Trim <"<" | ">" >, Strings .Split <":" >]>>,
Tuples .ToUnion ,
Objects .FromEntries ,
Objects .MapValues <
Match <[Match .With <"string" , string >, Match .With <"number" , number >]>
>
]
>;
API
Core
Function
Tuples
Create<X> -> [X]
Partition<Tuple>
IsEmpty<Tuple>
Zip<...Tuple[]>
ZipWith<Fn, ...Tuple[]>
Sort<Tuple>
Head<Tuple>
Tail<Tuple>
At<N, Tuple>
Last<Tuple>
FlatMap<Fn, Tuple>
Find<Fn, Tuple>
Drop<N, Tuple>
Take<N, Tuple>
TakeWhile<Fn, Tuple>
GroupBy<Fn, Tuple>
Join<Str, Tuple>
Map<Fn, Tuple>
Filter<Fn, Tuple>
Reduce<Fn, Init, Tuple>
ReduceRight<Fn, Init, Tuple>
Reverse<Tuple>
Every<Fn, Tuple>
Some<Fn, Tuple>
SplitAt<N, Tuple>
ToUnion<Tuple>
ToIntersection<Tuple>
Prepend<X, Tuple>
Append<X, Tuple>
Concat<T1, T2>
Min<Tuple>
Max<Tuple>
Sum<Tuple>
Object
Readonly<Obj>
Mutable<Obj>
Required<Obj>
Partial<Obj>
ReadonlyDeep<Obj>
MutableDeep<Obj>
RequiredDeep<Obj>
PartialDeep<Obj>
Update<Path, Fn | V, Obj>
Record<Key, Value>
Keys<Obj>
Values<Obj>
AllPaths<Obj>
Create<Pattern, X>
Get<Path, Obj>
FromEntries<[Key, Value]>
Entries<Obj>
MapValues<Fn, Obj>
MapKeys<Fn, Obj>
Assign<...Obj>
Pick<Key, Obj>
PickBy<Fn, Obj>
Omit<Key, Obj>
OmitBy<Fn, Obj>
CamelCase<Obj>
CamelCaseDeep<Obj>
SnakeCase<Obj>
SnakeCaseDeep<Obj>
KebabCase<Obj>
KebabCaseDeep<Obj>
Union
String
Length<Str>
TrimLeft<Str>
TrimRight<Str>
Trim<Str>
Join<Sep, Str>
Replace<From, To, Str>
Slice<Start, End, Str>
Split<Sep, Str>
Repeat<N, Str>
StartsWith<S, Str>
EndsWith<E, Str>
ToTuple<Str>
ToNumber<Str>
ToString<Str>
Prepend<Start, Str>
Append<End, Str>
Uppercase<Str>
Lowercase<Str>
Capitalize<Str>
Uncapitalize<Str>
SnakeCase<Str>
CamelCase<Str>
KebabCase<Str>
Compare<Str, Str>
Equal<Str, Str>
NotEqual<Str, Str>
LessThan<Str, Str>
LessThanOrEqual<Str, Str>
GreaterThan<Str, Str>
GreaterThanOrEqual<Str, Str>
Number
Add<N, M>
Multiply<N, M>
Subtract<N, M>
Negate<N>
Power<N, M>
Div<N, M>
Mod<N, M>
Abs<N>
Compare<N, M>
GreaterThan<N, M>
GreaterThanOrEqual<N, M>
LessThan<N, M>
LessThanOrEqual<N, M>
Boolean